<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
 * Description: Login controller class
 */
class Login extends CI_Controller{
    
    function __construct(){
        parent::__construct();
    }
    
    public function index($msg = NULL){
        // Load our view to be displayed
        // to the user
    	$this->load->helper('url');
        $data['msg'] = $msg;
        $this->load->view('templates/header_admin_start', $data);
        $this->load->view('admin_login', $data);
        $this->load->view('templates/footer');
    }
    
    public function process(){
        // Load the model
        $this->load->helper('url');
        $this->load->model('news_model');
        // Validate the user can login
        $result = $this->news_model->login();
        // Now we verify the result
        if(! $result){
            // If user did not validate, then show them login page again
            $msg = 'Invalid username and/or password. <br>';
            $this->index($msg);
        }
        else{
            //	 If user did validate, 
            // Send them to members area
            redirect('welcome_admin/index/home');
        }    
    }
    
    public function not_loggedin()
    {
    	$msg = 'Please login to continue. <br>';
    	$this->index($msg);
    }
    
    public function forgot_password()
    {
    	$data['failure_message'] = NULL;
    	$this->load->helper('url');
        $this->load->model('news_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $data['title'] = 'Forgot password';
        
        $this->form_validation->set_rules('username', 'text', 'required');
        
        if ($this->form_validation->run() === FALSE)
        {
        	$this->load->view('templates/header_admin_start', $data);
        	$this->load->view('admin_forgot_password', $data);
        	$this->load->view('templates/footer');
        
        }
        
        else
        {
        	if ($this->news_model->forgot_password())
        	{
        		$this->load->view('templates/header_admin_start', $data);
        		$data['msg'] = NULL;
        		$this->load->view('admin_login', $data);
        		$this->load->view('templates/footer');
        	}
        
        	else
        	{
        		$data['failure_message'] = "<font color = 'red'>Invalid username </font><p>";
        		$this->load->view('templates/header_admin_start', $data);
        		$this->load->view('admin_forgot_password', $data);
        		$this->load->view('templates/footer');
        	}
        }
        
    }
    
}
?>
